package com.example.Controller;

import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Result;
import com.example.ResultCodeEnum;
import com.example.Service.SysRoleService;
import com.example.Service.SysUserService;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation("分页")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, SysUserQueryVo sysUserQueryVo) {
        Page<SysUser> page1 = new Page<>(page, limit);
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(sysUserQueryVo.getCreateTimeBegin())) {
            lambdaQueryWrapper.ge(SysUser::getCreateTime, sysUserQueryVo.getCreateTimeBegin());

        }
        if (!StringUtils.isEmpty(sysUserQueryVo.getCreateTimeEnd())) {
            lambdaQueryWrapper.le(SysUser::getCreateTime, sysUserQueryVo.getCreateTimeEnd());

        }
        if (!StringUtils.isEmpty(sysUserQueryVo.getKeyword())) {
            lambdaQueryWrapper.like(SysUser::getUsername, sysUserQueryVo.getKeyword());

        }
        IPage<SysUser> page2 = sysUserService.page(page1, lambdaQueryWrapper);
        return Result.build(page2, ResultCodeEnum.SUCCESS);

    }

    @GetMapping("/get/{id}")
    public Result<SysUser> get(@PathVariable("id") Integer id) {
        SysUser sysUser = sysUserService.getById(id);
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/save")
    public Result add(@RequestBody SysUser sysUser) {
        sysUserService.save(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PutMapping("/update")
    public Result update(@RequestBody SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/remove/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        sysUserService.removeById(id);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }





    @ApiOperation("更改用户状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        SysUser sysUser=sysUserService.getById(id);
        sysUser.setStatus(status);
        sysUserService.updateById(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
